package Concurrency2.Chapter08_Pattern.master_worker;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class Task<R> {
    private static AtomicInteger index = new AtomicInteger(0);
    //回调函数
    public Consumer<Task<R>> resultAction;
    // 任务id, 在构造函数中创建
    private int id;
    private int workerId;
    //计算结果
    private R result = null;

    public Task() {
        this.id = index.getAndIncrement();
    }

    //模板函数
    public void execute(){
        //获取执行结果
        this.result = this.doExecute();
        // 执行回调函数
        resultAction.accept(this);
    }

    // 钩子函数
    protected R doExecute() {
        return null;
    }


    public void setWorkerId(int workerId) {
        this.workerId = workerId;
    }

    public int getId() {
        return id;
    }

    public int getWorkerId() {
        return workerId;
    }

    public R getResult() {
        return result;
    }
}
